﻿//JumbotCms_4.1 Final
using System;
using System.Data;
using System.Web;
using System.Web.UI.WebControls;
using Jumbot.Common;
using JumbotCms.Common;
namespace JumbotCms.WebFile.Admin
{
    public partial class _dbaccessajax : JumbotCms.API.AdminCenter
    {
        private string _operType = string.Empty;
        private string _response = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!CheckFormUrl())
            {
                Response.Write("提交有误");
                Response.End();
            }
            Admin_Load("0000", "json");
            this._operType = q("oper");
            switch (this._operType)
            {
                case "ajaxCompact":
                    ajaxCompact();
                    break;
                case "ajaxBackup":
                    ajaxBackup();
                    break;
                case "ajaxRestore":
                    ajaxRestore();
                    break;
                default:
                    DefaultResponse();
                    break;
            }
            Response.Write(this._response);
        }

        private void DefaultResponse()
        {
            this._response = JsonResult(0, "未知操作");
        }
        private void ajaxCompact()
        {
            doh.Dispose();
            string tempPath = Server.MapPath(Application["Database:dbType"].ToString());
            string _dbPath = Jumbot.Common.DirFile.GetFolderPath(tempPath);
            string _dbName = Jumbot.Common.DirFile.GetFileName(tempPath);
            if (!System.IO.File.Exists(tempPath))
            {
                this._response = JsonResult(0, "目标数据库不存在");
                return;
            }
            try
            {
                string temp = _dbPath + DateTime.Now.Year.ToString("yyyyMMddHHmmssffff") + ".bak";
                string temp2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + temp;
                string _dbPath2 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + tempPath;
                JRO.JetEngineClass jt = new JRO.JetEngineClass();
                jt.CompactDatabase(_dbPath2, temp2);
                System.IO.File.Copy(temp, tempPath, true);
                System.IO.File.Delete(temp);
            }
            catch (Exception)
            {
                this._response = JsonResult(0, "其它用户连接数据库");
                return;
            }
            this._response = JsonResult(1, "数据库压缩成功");
        }
        private void ajaxBackup()
        {
            doh.Dispose();
            string tempPath = Server.MapPath(Application["Database:dbType"].ToString());
            string _dbPath = Jumbot.Common.DirFile.GetFolderPath(tempPath);
            string _dbName = Jumbot.Common.DirFile.GetFileName(tempPath);
            try
            {
                System.IO.File.Copy(_dbPath + "\\" + _dbName, _dbPath + "\\..\\databackup\\" + f("dbname"), true);
            }
            catch (Exception)
            {
                this._response = JsonResult(0, "数据库正在被使用");
                return;
            }
            this._response = JsonResult(1, "数据库备份成功");
        }
        private void ajaxRestore()
        {
            doh.Dispose();
            string tempPath = Server.MapPath(Application["Database:dbType"].ToString());
            string _dbPath = Jumbot.Common.DirFile.GetFolderPath(tempPath);
            string _dbName = Jumbot.Common.DirFile.GetFileName(tempPath);
            if (System.IO.File.Exists(_dbPath + "\\..\\databackup\\" + f("dbname")))
            {
                try
                {
                    System.IO.File.Copy(_dbPath + "\\..\\databackup\\" + f("dbname"), _dbPath + "\\" + _dbName, true);
                }
                catch (Exception)
                {
                    this._response = JsonResult(0, "数据库正在被使用");
                    return;
                }
            }
            else
            {
                this._response = JsonResult(0, "原始数据库不存在");
                return;
            }
            this._response = JsonResult(1, "数据库恢复成功");
        }
    }
}